import axios from "axios";
import { Message } from "element-ui";
import store from "/src/store/index"

// axios.defaults.baseURL = 'http://172.16.10.171:41612'

axios.interceptors.request.use(request => {

    // Message({
    //   message: store.state.sessionId,
    //   type: "info",
    //   duration: 1000,
    // });
    //   return false
    // }

    request.headers['Authorization'] = store.state.sessionId
    return request

})

axios.interceptors.response.use(response => {
    // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
    // 否则的话抛出错误
    if (response.status === 200) {
      return Promise.resolve(response.data);
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是2开头的的情况
  // 这里可以跟你们的后台开发人员协商好统一的错误状态码
  // 然后根据返回的状态码进行一些操作，例如登录过期提示，错误提示等等
  // 下面列举几个常见的操作，其他需求可自行扩展
  error => {
    if (error.response.status) {
      switch (error.response.status) {
        // 401: 未登录
        // 未登录则跳转登录页面，并携带当前页面的路径
        // 在登录成功后返回当前页面，这一步需要在登录页操作。
        case 401:

          break;

        // 403 token过期
        // 登录过期对用户进行提示
        // 清除本地token和清空vuex中token对象
        // 跳转登录页面
        case 403:
          Message({
            message: "登录过期，请重新登录",
            type: 'warning',
            duration: 3000,
          });
          break;
        // 404请求不存在
        case 404:
          Message({
            message: "网络请求不存在",
            type: 'error',
            duration: 3000,
          });
          break;
        // 其他错误，直接抛出错误提示
        default:
          Message({
            message: error.response.data.message,
            type: 'error',
            duration: 3000,
          });
      }
      return Promise.reject(error.response);
    }
  });

/*
* 后台有物理分页 考虑到数据量不大 直接写死
* */
let fixedSize = 1000000000

const login = (username, password) => {
  /* 简单登录接口 */
  return axios.post('/api/login/toLogin', {
    username: username,
    password: password
  })
}

const editUser = (method, id, o) => {
  return axios.post(`/api/user/editUser/${method}`, {
    id: id,
    t: o
  })
}

const getAllUsers = () => {
/* 获取所有的用户信息 */
  return axios.post('/api/user/getAllUsers')
}


const editShapeDesc = (method, id, o) => {
  return axios.post(`/api/shapeDesc/editShapeDesc/${method}`, {
    id: id,
    t: o
  })
}

const getShapeDesc = () => {
  return axios.post('/api/shapeDesc/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}

/* =====================================================  */

const editNutrition = (method, id, o) => {
  return axios.post(`/api/nutritionContain/editNutritionContain/${method}`, {
    id: id,
    t: o
  })
}

const getNutrition = () => {
  return axios.post('/api/nutritionContain/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}

/* =====================================================  */

const editIngredient = (method, id, o) => {
  return axios.post(`/api/ingredient/editIngredient/${method}`, {
    id: id,
    t: o
  })
}

const getIngredient = () => {
  return axios.post('/api/ingredient/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}

/* =====================================================  */

const editIngredientCheck = (method, id, o) => {
  return axios.post(`/api/sourceCheck/editSourceCheck/${method}`, {
    id: id,
    t: o
  })
}

const getIngredientCheck = () => {
  return axios.post('/api/sourceCheck/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}


/* =====================================================  */

const editProductMaintain = (method, id, o) => {
  return axios.post(`/api/productMaintain/editProductMaintain/${method}`, {
      id: id,
      t: o
    })
}

const getProductMaintain = () => {
  return axios.post('/api/productMaintain/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}

/* =====================================================  */

const editPackMaintain = (method, id, o) => {
  return axios.post(`/api/packMaintain/editPackMaintain/${method}`, {
    id: id,
    t: o
  })
}

const getPackMaintain = () => {
  return axios.post('/api/packMaintain/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}


/* =====================================================  */

const getDocument = (t) => {
  return axios.post(`/api/document/getAll/${t}`, {
    type: t
  })
}

/* =====================================================  */

const editDocument = (type, id, method, details, fileInfo) => {
  return axios.post(`/api/document/${method}`, {
    type: type,
    fileInfo: fileInfo,
    details: details,
    id: id
  })
}

/* =====================================================  */

const editChiliStudy = (method, id, o) => {
  return axios.post(`/api/chiliStudy/editChiliStudy/${method}`, {
    id: id,
    t: o
  })
}

const getChiliStudy = () => {
  return axios.post('/api/chiliStudy/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}

/* =====================================================  */

const editFlowerChiliStudy = (method, id, o) => {
  return axios.post(`/api/flowerChiliStudy/editFlowerChiliStudy/${method}`, {
    id: id,
    t: o
  })
}

const getFlowerChiliStudy = () => {
  return axios.post('/api/flowerChiliStudy/getDataByPage', {
    "currentPage": 1,
    "size": fixedSize
  })
}

/* =====================================================  */

const getAllBackup = () => {
  return axios.post('/api/backup/getAllBackup')
}

const backup = (tableName, details) => {
  return axios.post(`/api/backup/backup/${tableName}`, {
    details: details
  })
}


const rollback = (id) => {
  return axios.post('/api/backup/rollback', {
    id: id
  })
}

const editBackup = (tableName, method, id, o) => {
  return axios.post(`/api/backup/edit/${tableName}/${method}`, {
      id: id, t: o
  })
}
/* =====================================================  */

const getTemplate = (tableName) => {
  return axios.get(`/api/port/exportDataTemplate/${tableName}`)
}

const examPreview = (tableName, file) => {
  return axios.post(`/api/port/previewImport/${tableName}`, {
    multipartFile: file
  }, {

  })
}

const saveBatch = (tableName, data) => {
  return axios.post(`/api/${tableName}/saveBatch`, {
    objectList: data
  })
}

const exportData = (tableName) => {
  return axios.get(`/api/port/exportData/${tableName}`)
}


const getAuthorization = (userId) => {
  return axios.get(`/api/authorization/getUserAuthorization/${userId}`)
}

const editAuthorization = (method, id, o) => {
  return axios.post(`/api/authorization/editAuthorization/${method}`, {
    id: id,
    t: o
  })
}


const api = {
  login,

  editUser,
  getAllUsers,

  editShapeDesc,
  getShapeDesc,

  editNutrition,
  getNutrition,

  editIngredient,
  getIngredient,

  editIngredientCheck,
  getIngredientCheck,

  editProductMaintain,
  getProductMaintain,

  editPackMaintain,
  getPackMaintain,

  editDocument,
  getDocument,

  editChiliStudy,
  getChiliStudy,

  editFlowerChiliStudy,
  getFlowerChiliStudy,

  // ===========
  getAllBackup,
  rollback,
  backup,
  editBackup,

  // ===========
  getTemplate,
  examPreview,
  saveBatch,

  exportData,

  // ====
  getAuthorization,
  editAuthorization
}

export { api }
